/**
 *  *2010-3-26 下午04:19:05  add 
 */
package com.base.myproject.client.busi.service;

import java.util.Date;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.HoursTable;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.TimeField;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;

/**
 * @author Administrator 2010-3-26 javazgw@gmail.com
 */
public class ReservationDialog {
	Dialog dialog ;
	BaseStore<BusiObject> store = new BaseStore<BusiObject>();
	 final HoursTable ht = new HoursTable();
	 TimeField C_data4;
	 TextField<String> C_num11  = new TextField<String>(); //会员卡号
	 TextField<String> C_num10  = new TextField<String>(); //会员手机
	 TextField<String> C_num3  = new TextField<String>(); //ccode
	 BaseComboBoxForm<BaseModelData> C_num2  ; //技师 bcode
		TextField<String> C_num7  = new TextField<String>(); //serviceitem
		TextArea C_num5  = new TextArea(); //mark
		  NumberField C_num13  = new NumberField(); //时间 deelhour

		Date daynow = new Date();
		DateTimeFormat fmt = DateTimeFormat.getFormat("yyyy-M-d H:mm:ss");
	public ReservationDialog() {
		init();
	}

	private void init() {
		// TODO Auto-generated method stub

	}

	public void show() {
		
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("ccode");
		type.getField("ccode").setType(String.class);
		type.addField("Reserdate");
		type.getField("Reserdate").setType(Date.class);
		type.addField("mark");
		type.getField("mark").setType(String.class);
		type.addField("prebcode");
		type.getField("prebcode").setType(String.class);
		type.addField("serviceitem");
		type.getField("serviceitem").setType(String.class);
		type.addField("predate");
		type.getField("predate").setType(Date.class);
		type.addField("busistatus");
		type.getField("busistatus").setType(String.class);
		type.addField("s_mobil");
		type.getField("s_mobil").setType(String.class);
		type.addField("s_cardno");
		type.getField("s_cardno").setType(String.class);
		type.addField("busicode");
		type.getField("busicode").setType(String.class);
		type.addField("deelhours");
		type.getField("deelhours").setType(Double.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("Reservation");
		info.setKeyname(new String[] { "icode" });
		store.setType(type);
		store.setStoreinfo(info);

		
		setdefault();
		 dialog = new Dialog();
		dialog.setHeading("客户预约");
		dialog.add(createForm());
		
//		dialog.setSize(805, 330);
		dialog.setAutoHeight(true);
		dialog.setWidth(805);
		dialog.setTitle("客户预约");
		dialog.getButtonById(Dialog.OK).setText("提交确认");
		dialog.getButtonById(Dialog.OK).addListener(Events.Select, getListener());
		
		dialog.show();
		
	}

	private void setdefault() {
//		 busi =SystemMessage.busi[9];
		store.addDefault("busicode", SystemMessage.busi[9]);
		store.addDefault("prebcode", Env.getEnvbcode());
		store.addDefault("predate", fmt.format(new Date()));
		
	}

	public void close() {

		form = null;
		store = null;
		dialog.hide();
		dialog = null;
	}

	BaseFormPanel form = null;

	BaseFormPanel createForm() {
		 form = new BaseFormPanel(store,false);
		TableLayout tl = new TableLayout(6);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		
		TableData td2 = new TableData();
		td2.setRowspan(1);
		td2.setColspan(6);
		td2.setHorizontalAlign(HorizontalAlignment.LEFT);
		td2.setVerticalAlign(VerticalAlignment.TOP);
		td2.setPadding(2);
		
		
		TableData td3 = new TableData();
		td3.setRowspan(1);
		td3.setColspan(5);
		td3.setHorizontalAlign(HorizontalAlignment.LEFT);
		td3.setVerticalAlign(VerticalAlignment.TOP);
		td3.setPadding(2);
		
		form.setLayout(tl);
		form.setHeaderVisible(false);

//		form.add(new Label("icode:"),td);
//		 NumberField C_num1  = new NumberField(); 
//		C_num1.setPropertyEditorType(Long.class); 
//		C_num1.setName("icode");
//		form.add(C_num1,td);
		form.add(new Label("会员卡号:"),td);
		
		C_num11.setName("s_cardno");
		form.add(C_num11,td);
		form.add(new Label("会员手机:"),td);
		
		C_num10.setName("s_mobil");
		form.add(C_num10,td);
		form.add(new Label("客户代码:"),td);
		
		C_num3.setName("ccode");
		form.add(C_num3,td);
		
		form.add(new Label("技师:"),td);
		C_num2 =new BaseComboBoxForm<BaseModelData>("bcode","bname","bcode","",true);
		C_num2.setName("bcode");
		form.add(C_num2,td);
		form.add(new Label("服务项目:"),td);

		C_num7.setName("serviceitem");
		form.add(C_num7,td);
		
		//TimeField time = new TimeField(); 
		form.add(new Label("预计到达时间:"),td);
		 C_data4  = new TimeField(); 
		 
		//C_data4.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-M-d")); 
		C_data4.setName("Reserdate");
		form.add(C_data4,td);
		//TimeField time = new TimeField(); 
		form.add(new Label("预计耗时:"),td);
		
		C_num13.setPropertyEditorType(Double.class); 
		C_num13.setName("deelhours");
		form.add(C_num13,td);
		
		

	
//		form.add(new Label("制单人:"),td);
//		TextField<String> C_num6  = new TextField<String>(); 
//		C_num6.setName("prebcode");
//		form.add(C_num6,td);

//		form.add(new Label("predate:"),td);
//		 DateField C_data8  = new DateField(); 
//		C_data8.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-M-d")); 
//		C_data8.setName("predate");
//		form.add(C_data8,td);
//		form.add(new Label("busistatus:"),td);
//		TextField<String> C_num9  = new TextField<String>(); 
//		C_num9.setName("busistatus");
//		form.add(C_num9,td);
		

//		form.add(new Label("busicode:"),td);
//		TextField<String> C_num12  = new TextField<String>(); 
//		C_num12.setName("busicode");
//		form.add(C_num12,td);
		
		C_num13.setValue(1);
		C_num13.setAllowBlank(false);
		C_data4.setAllowBlank(false);
		C_num10.setAllowBlank(false);
		C_data4.setIncrement(30);
		C_num3.setEnabled(false);
		
		
		C_num13.addListener(Events.Change, getListener());
		C_num2.addListener(Events.Change, getListener());
		C_num13.addListener(Events.Change, getListener());
		C_data4.addListener(Events.Change, getListener());
		C_num11.addListener(Events.Change, getListener());
		
		
		//form.add(new Label(""), td);
		//form.add(new Label(""), td);
		form.add(new Label(""), td);
		form.add(new Label(""), td);
		form.add(new Label(""), td);
		form.add(new Label(""), td);
		//form.add(new Label(""), td);
		
		form.add(new HTML("<hr>备注(可以记录客人的要求信息):"),td2);
		
		C_num5.setName("mark");
		C_num5.setWidth(375);
		form.add(C_num5,td2);
		form.add(new HTML("<hr> 选择的人员当日被预约时间表。根据具体工作时间长度，统计可能有误差！"),td2);
		form.add(ht,td2);
		//form.inited();
		
		return form;
	};

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(dialog.getButtonById(Dialog.OK))) {
								if(form.isValid())
								{
									BusiObject md = new BusiObject();
										if(C_num2.getValue()!=null)
											md.set("bcode", C_num2.getValue().get("code"));
										
										md.set("s_cardno", C_num11.getValue());
										md.set("s_mobil", C_num10.getValue());
										md.set("ccode", C_num3.getValue());
										md.set("serviceitem", C_num7.getValue());
//										md.set("s_cardno", C_num11.getValue());
//										md.set("s_cardno", C_num11.getValue());
//										md.set("ccode", "2");
										md.set("mark", C_num5.getValue());
										md.set("deelhours", C_num13.getValue());
//										md.set("mark", C_num5.getValue());
//										md.set("mark", C_num5.getValue());
										
										GWT.log("date "+fmt.format(C_data4.getValue().getDate()), null);
										if(C_data4.getValue()!=null)
											md.set("Reserdate", fmt.format(C_data4.getValue().getDate()));
										store.add(md);
										post();
								}
							}
						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals(C_num2)) {
								//ht.clear();
								ht.removeexcept("now");
								 com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
								.create(com.base.myproject.client.tools.GreetingService.class);
								greetingService.getDataSet("select Reserdate,deelhours,icode from reservation where datepart(Hour,Reserdate)>=8  and datediff(Day,getdate(),Reserdate)=0 and bcode='"+C_num2.getValueCode()+"'", getAsyncCallback());

							}
							//耗时
							else if (be.getSource().equals(C_num13)) {
								
								cal();
								
							}
							//到达时间
							else if (be.getSource().equals(C_data4)) {
								
								cal();
								
							}
							//会员卡号
							else if (be.getSource().equals(C_num11)) {
								//if(C_num11.getValue()!=null)
								{
								 com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
								.create(com.base.myproject.client.tools.GreetingService.class);
								greetingService.getAssociator(C_num11.getValue(),4,getAsyncCallback_Associator());
								}
							}
							
							
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
	
	private AsyncCallback<DataSet> getAsyncCallback()
	{
	AsyncCallback<DataSet> callback = new AsyncCallback<DataSet>()
	{

		@Override
		public void onFailure(Throwable caught) {
			MessageBox.alert("错误", "该错误发生于网络不稳定!请重新操作", null);
			
		}

		@Override
		public void onSuccess(DataSet result) {
			for(int i=0;i<result.getRowCount();i++)
			{
				Date d = fmt.parse(result.getValue(i, "Reserdate"));
				
				String hours = result.getValue(i, "deelhours");
				double hour = (hours==null||hours.equals(""))?1:Double.parseDouble(hours);
				hour = hour*2;
				int d_hour = d.getHours();
				int d_min  = (d.getMinutes()==30)?1:0;
		
				
				GWT.log("hour="+hour+" d_hour="+d_hour+" d_min="+d_min+" ss"+(d_hour-8)*2,null);
				for(int j=0;j<hour;j++)
				{
					ht.setValue((d_hour-8)*2+d_min+j, result.getValue(i, "icode"), "");
				}
				
			}
		}
		
	};
	return callback;
	}
	
	private AsyncCallback<Associator> getAsyncCallback_Associator()
	{
		AsyncCallback<Associator> callback = new AsyncCallback<Associator>()
		{

			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert("错误", "该错误发生于网络不稳定!请重新操作", null);
				
			}

			@Override
			public void onSuccess(Associator result) {
			
				C_num10.setValue(result.getPhone());
				C_num3.setValue(result.getCcode());
			}
			
		};
		
		return callback;
	}
	
	
	private void cal()
	{
//		C_num13,C_data4
		
		ht.remove("now");
		
		if(C_data4.getValue()==null)
			return ;
		double hour = C_num13.getValue().doubleValue();
		hour = hour*2;
		int d_hour = C_data4.getValue().getHour();
		int d_min = (C_data4.getValue().getMinutes()==30)?1:0;
		for(int j=0;j<hour;j++)
		{
			ht.setValue((d_hour-8)*2+d_min+j, "now", "");
		}
	}
	/**
	 * 
	 */
	public void post()
	{
		
	}
}
